package com.lightningtrade.controller;

import com.lightningtrade.domain.SysProduct;
import com.lightningtrade.log.MethodLog;
import com.lightningtrade.log.annotaion.Log;
import com.lightningtrade.log.enums.BusinessType;
import com.lightningtrade.service.SysProductService;
import com.lightningtrade.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Api(tags = "商品管理-0312杜晟玮")
@RestController
@RequestMapping("/product")
public class SysProductController {

    @Autowired
    private SysProductService sysProductService;

    @Log(title = "添加商品", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加商品", notes = "添加新的商品")
    @PostMapping("/add")
    public AjaxResult addProduct(@Validated @RequestBody SysProduct sysProduct) {
        int result = sysProductService.addProduct(sysProduct);
        if (result > 0) {
            return AjaxResult.success("商品添加成功");
        }
        return AjaxResult.error("商品添加失败");
    }

    @ApiOperation(value = "更新商品信息", notes = "更新商品的名称、价格、库存等信息")
    @PutMapping("/update")
    public AjaxResult updateProduct(@RequestBody SysProduct sysProduct) {
        int result = sysProductService.updateProduct(sysProduct);
        if (result > 0) {
            return AjaxResult.success("商品信息更新成功");
        }
        return AjaxResult.error("商品信息更新失败");
    }

    @ApiOperation(value = "删除商品", notes = "根据商品ID删除商品")
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteProduct(@PathVariable Integer id) {
        int result = sysProductService.deleteProduct(id);
        if (result > 0) {
            return AjaxResult.success("商品删除成功");
        }
        return AjaxResult.error("商品删除失败");
    }

    @ApiOperation(value = "按商品id查询商品", notes = "根据商品ID查询商品")
    @GetMapping("/getProductById/{id}")
//    @PreAuthorize("hasRole('seller')")
    public AjaxResult getProduct(@PathVariable Integer id) {
        SysProduct sysProduct = sysProductService.getProductById(id);
        if (sysProduct != null) {
            System.out.println(sysProduct.getUpdatedAt());
            return AjaxResult.success("查询成功", sysProduct);
        }
        return AjaxResult.error("商品未找到");
    }

    @MethodLog
    @ApiOperation(value = "按商品名称查询商品", notes = "根据商品名称查询商品")
    @GetMapping("/getProductByName/{productName}")
//    @PreAuthorize("hasRole('seller')")
    public AjaxResult getProduct(@PathVariable String productName) {
         return sysProductService.getProductByName(productName);
    }

    @Log(title = "查询所有商品", businessType = BusinessType.OTHER)
    @ApiOperation(value = "查询所有商品", notes = "查询所有商品")
    @GetMapping("/list")
    public AjaxResult getAllProducts() {
        return AjaxResult.success("查询成功", sysProductService.getAllProducts());
    }

    @ApiOperation(value = "上下架商品", notes = "根据商品ID更新商品的状态")
    @PutMapping("/status/{id}")
    //    @PreAuthorize("hasRole('seller')")
    public AjaxResult updateProductStatus(@PathVariable Integer id, @RequestParam Integer status) {
        int result = sysProductService.updateProductStatus(id, status);
        if (result > 0) {
            return AjaxResult.success("商品状态更新成功");
        }
        return AjaxResult.error("商品状态更新失败");
    }

    @Value("${file.upload.path}")
    private String uploadPath;


    @ApiOperation(value = "上传商品图片", notes = "上传商品图片")
    @PostMapping("/upload")
    public AjaxResult uploadProductImage(@RequestPart("file") MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("上传文件不能为空");
        }

        String fileName = file.getOriginalFilename();
        File path = new File(uploadPath);
        if (!path.exists()) {
            path.mkdirs();
        }

        if(!path.exists()) {
            path.mkdirs();
        }

        File dest = new File(path + "/" + fileName);
        try {
            file.transferTo(dest);
            String url = uploadPath + fileName;
            return AjaxResult.success("上传成功").put("url", url);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败");
        }
    }
}
